using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;

namespace WarmMeUp.Features.WarmMeUp.WebApp
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("7230c3c9-6b51-439e-bb68-5b9b316f5b42")]
    public class WarmMeUpEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApplication = properties.Feature.Parent as SPWebApplication;

            RemoveJobDefinition(webApplication);
            // Deploying new WarmUpJobDefinition
            InstallJobDefinition(webApplication);
            // Adding custom property
            AddProperty(webApplication);
        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApplication = properties.Feature.Parent as SPWebApplication;

            // Removing WarmUpJobDefinition
            RemoveJobDefinition(webApplication);
            // Removing custom property
            RemoveProperty(webApplication);
        }


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}

        /// <summary>
        /// Install a new WarmUpJobDefinition
        /// </summary>
        /// <param name="webApplication"></param>
        private void InstallJobDefinition(SPWebApplication webApplication)
        {
            // Creating new Job instance
            WarmMeUpJob job = new WarmMeUpJob(Constants.JobName + " " + webApplication.Name, webApplication, null, SPJobLockType.Job);

            // Creating Job scheduling
            SPMinuteSchedule schedule = new SPMinuteSchedule();
            schedule.Interval = 15;             // Each 15 minutes
            schedule.BeginSecond = 0;
            schedule.EndSecond = 59;
            job.Schedule = schedule;

            // Adding job to JobDefinitions
            webApplication.JobDefinitions.Add(job);
        }

        /// <summary>
        /// Search and remove WarmUpJobDefinition if exists
        /// </summary>
        private void RemoveJobDefinition(SPWebApplication webApplication)
        {
            // Browsing all JobDefinition
            foreach (SPJobDefinition job in webApplication.JobDefinitions)
            {
                // Current job is a WarmUpJobDefinition ?
                if (job is WarmMeUpJob)
                {
                    // Removing job
                    job.Delete();
                }
            }
        }

        /// <summary>
        /// Add the WarmUp custom property
        /// </summary>
        /// <param name="webApplication"></param>
        private void AddProperty(SPWebApplication webApplication)
        {
            // Does the property already exists ?
            if (!webApplication.Properties.ContainsKey(Constants.AdditionalUrls))
            {
                // No, we add it and save changes
                webApplication.Properties.Add(Constants.AdditionalUrls, string.Empty);
                webApplication.Update();
            }
        }

        /// <summary>
        /// Remove the WarmUp property
        /// </summary>
        /// <param name="webApplication"></param>
        private void RemoveProperty(SPWebApplication webApplication)
        {
            // Does the property exists ?
            if (webApplication.Properties.ContainsKey(Constants.AdditionalUrls))
            {
                // Yes, we remove it and save changes
                webApplication.Properties.Remove(Constants.AdditionalUrls);
                webApplication.Update();
            }
        }
    }
}
